//#ifndef encode_js
//#define encode_js 1

var encode = {

  // encode a number as a base 85 string
  fromNumber : function (n) {
    for (var r=''; n;) {
      r += String.fromCharCode((n%85) + 35);
      n = Math.floor(n/85);
    }
    return r.replace(/\\/g,'~');
  },
  
  // encode a byte array as a base 85 string
  fromArray : function (a) {
    var i, r='', q=0, e, l=a.length;
    for (i=0; i<l; i++) {
      q += a[i] * Math.pow(256, i%4);
      if ((i+1)%4) continue;
      e = this.fromNumber(q); 
      r += e + (e.length < 5 ? ' ' : '');
      q = 0;
    }
    if (q) r += this.fromNumber(q);
    return r.trim();
  },
  
  // encode a text string as a base 85 string
  //#ifdef ENCODE_STRING
  fromString : function (str) {
    var i, a=[], l=str.length, r='';
    for (i=0; i<l; i++) a.push(str.charCodeAt(i));
    return this.fromArray(a);
  }
  //#endif
  
};

//#endif

